﻿using AutoMapper;
using Modules;
using Modules.Corp;
using Modules.Person;
using Modules.Project;
using PmSoft;
using static Modules.Payroll.PayrollConstants;
using static Modules.System.Flow.FlowConstants;

namespace ManagementApi.Models.Payroll
{
    public class PayrollDto
    {
        /// <summary>
        /// 主键
        ///</summary>
        public int Id { get; set; }

        /// <summary>
        /// 编号
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 工资单名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 项目Id
        ///</summary>
        public int ProjectId { get; set; }

        /// <summary>
        /// 企业id
        ///</summary>
        public int CorpId { get; set; }

        /// <summary>
        /// 企业名称
        ///</summary>
        public string CorpName { get; set; }

        /// <summary>
        /// 流水号
        ///</summary>
        public string? SerialNumber { get; set; }

        /// <summary>
        /// 审核类型
        ///</summary>
        public 审核类型 AuditType { get; set; }

        /// <summary>
        /// 审核类型
        /// </summary>
        public string AuditTypeName { get; set; }

        /// <summary>
        /// 审核状态
        ///</summary>
        public Modules.Payroll.PayrollConstants.审核状态 AuditStatus { get; set; }

        /// <summary>
        /// 审核状态
        ///</summary>
        public string AuditStatusName { get; set; }

        /// <summary>
        /// 支付方式
        ///</summary>
        public 工资计算方式? PayType { get; set; }

        /// <summary>
        /// 支付方式名称
        /// </summary>
        public string? PayTypeName { get; set; }

        /// <summary>
        /// 代发月份
        ///</summary>
        public int Month { get; set; }

        public string WageMonth { get; set; }

        /// <summary>
        /// 专户id
        ///</summary>
        public int SpecialAccountId { get; set; }

        /// <summary>
        /// 录入人数
        ///</summary>
        public int NumberPeople { get; set; }

        /// <summary>
        /// 当月计价总额
        /// </summary>
        public decimal? TotalValuation { get; set; }

        /// <summary>
        /// 申请金额
        ///</summary>
        public decimal? AmountApplied { get; set; }

        /// <summary>
        /// 代发总金额
        ///</summary>
        public decimal TotalAmount { get; set; }

        /// <summary>
        /// 成功金额
        ///</summary>
        public decimal? SuccessfulAmount { get; set; }

        /// <summary>
        /// 失败金额
        ///</summary>
        public decimal? FailureAmount { get; set; }

        /// <summary>
        /// 工资单类型
        /// </summary>
        public 工资单类型 WageType { get; set; }

        public string WageTypeName { get; set; }

        /// <summary>
        /// 创建时间
        ///</summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 开始日期
        ///</summary>
        public DateTime StartDate { get; set; }

        /// <summary>
        /// 结束日期
        ///</summary>
        public DateTime EndDate { get; set; }

        /// <summary>
        /// 是否提交审核
        /// </summary>
        public bool IsSubimt { get; set; } = false;

        /// <summary>
        /// 是否允许审核
        /// </summary>
        public bool IsAudit { get; set; } = false;

        /// <summary>
        /// 是否允许删除
        /// </summary>
        public bool IsDelete { get; set; } = false;

        /// <summary>
        /// 是否允许编辑
        /// </summary>
        public bool IsEdit { get; set; } = false;

        /// <summary>
        /// 是否创建银行代发批次
        /// </summary>
        public bool HasCreateBatch { get; set; }

        /// <summary>
        /// 银行代发批次号
        /// </summary>
        public string BatchNo { get; set; }

        /// <summary>
        /// 代发状态（0：未发放 1：待发放 2：部分代发 3:代发完成 -1:已撤销 ）
        /// </summary>
        public 代发状态? WageStatus { get; set; }

        /// <summary>
        /// 代发状态（0：未发放 1：待发放 2：部分代发 3:代发完成 -1:已撤销 ）
        /// </summary>
        public string WageStatusName { get; set; } = string.Empty;

        /// <summary>
        /// 成功人数
        /// </summary>
        public int? WageSuccessPersonCount { get; set; }

        /// <summary>
        /// 是否提交到银行代发
        /// </summary>
        public bool HasSubmit { get; set; }

        /// <summary>
        /// 扩展字段
        /// </summary>
        public string ProjectName { get; set; }
    }

    /// <summary>
    /// 扩展
    /// </summary>
    public static partial class WagePayrollExceptions
    {
        public static PayrollDto AsDto(this WagePayroll entity)
        {
            PayrollDto dto = new();
            var mapper = ServiceAccessor.GetService<IMapper>();
            dto.WageSuccessPersonCount = entity.WageSuccessPersonCount;
            mapper.Map(entity, dto);
            var corpService = ServiceAccessor.GetService<CorpService>();
            var corp = corpService.GetCorpInfo(entity.CorpId);

            var projectService = ServiceAccessor.GetService<ProjectService>();
            var projectInfo = projectService.GetProjectInfo(entity.ProjectId);
            if (projectInfo != null) dto.ProjectName = projectInfo.Name;
            if (corp != null)
                dto.CorpName = corp.Name;
            dto.PayTypeName = entity.PayType.GetDescription();
            dto.AuditStatusName = entity.AuditStatus.GetDescription();
            dto.AuditTypeName = entity.AuditType.GetDescription();
            dto.WageTypeName = entity.WageType.GetDescription();
            dto.WageMonth = dto.Month.ToString();
            dto.WageStatusName = dto.WageStatus?.GetDescription() ?? string.Empty;
            return dto;
        }

        public static PayrollDto AsDto(this WagePayroll entity, FlowEngine submitEngineCode, FlowEngine? auditEngineCode)
        {
            PayrollDto dto = new PayrollDto();
            var mapper = ServiceAccessor.GetService<IMapper>();
            mapper.Map(entity, dto);
            var corpService = ServiceAccessor.GetService<CorpService>();
            var corp = corpService.GetCorpInfo(entity.CorpId);
            if (corp != null)
                dto.CorpName = corp.Name;
            dto.PayTypeName = entity.PayType.GetDescription();
            dto.AuditStatusName = entity.AuditStatus.GetDescription();
            dto.AuditTypeName = entity.AuditType.GetDescription();
            dto.WageTypeName = entity.WageType.GetDescription();
            if (entity.FlowEngineCode == submitEngineCode && entity.AuditType == 审核类型.待提交)
            {
                dto.IsSubimt = true;
                dto.IsDelete = true;
                dto.IsEdit = true;
            }
            else if (entity.FlowEngineCode == auditEngineCode && entity.AuditType == 审核类型.待审核)
                dto.IsAudit = true;
            dto.WageMonth = dto.Month.ToString();
            dto.WageStatusName = dto.WageStatus?.GetDescription() ?? string.Empty;
            return dto;
        }
    }
}